package com.cyf.db.mapper;

import com.cyf.db.bean.PayOrder;
import com.cyf.wechat.network.bean.RoadPayOrderVo;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface PayOrderMapper {

    @Insert("insert into payorder(openid, userToken, fee,orderNo,transactionId,parkingId,feeType,plate,outTime,createdTime, " +
            "tradeType, payWay, payStatus, payTime, infoId, infoType, month, feeEndTime) "
            + "values(#{payOrder.openid}, #{payOrder.userToken}, #{payOrder.fee}, #{payOrder.orderNo}, #{payOrder.transactionId}, "
            + "#{payOrder.parkingId}, #{payOrder.feeType}, "
            + "#{payOrder.plate}, #{payOrder.outTime}, #{payOrder.createdTime}, #{payOrder.tradeType}, #{payOrder.payWay}, " +
            "#{payOrder.payStatus}, #{payOrder.payTime}, #{payOrder.infoId}, #{payOrder.infoType}, #{payOrder.month}, #{payOrder.feeEndTime})")
    @SelectKey(statement = "select LAST_INSERT_ID()", keyProperty = "payOrder.id", before = false, resultType = int.class)
    void insertPayOrder(@Param("payOrder") PayOrder payOrder);


    @Insert("insert into payorder(openid, userToken, orderNo,transactionId,parkingId,feeType,plate,outTime,createdTime, " +
            "tradeType, payWay, payStatus, payTime, infoId, infoType, month, feeEndTime, npsMAccountId, apiUserId, fromThird,couponId,localFeegroupId," +
            "fee,shouldFee,cardFee,discountFee,freeFee,couponFee, recordOrderId, status, " +
            "serviceFee, isv, aliUserId, send,deviceId, piType, actualFee) " +
            "values(#{payOrder.openid}, #{payOrder.userToken}, #{payOrder.orderNo}, #{payOrder.transactionId}, " +
            "#{payOrder.parkingId}, #{payOrder.feeType},#{payOrder.plate}, #{payOrder.outTime}, #{payOrder.createdTime}, " +
            "#{payOrder.tradeType}, #{payOrder.payWay}, #{payOrder.payStatus}, #{payOrder.payTime}, " +
            "#{payOrder.infoId}, #{payOrder.infoType}, #{payOrder.month}, #{payOrder.feeEndTime}, #{payOrder.npsMAccountId}, " +
            "#{payOrder.apiUserId}, #{payOrder.fromThird},#{payOrder.couponId},#{payOrder.localFeegroupId}, " +
            "#{payOrder.fee}, #{payOrder.shouldFee}, #{payOrder.cardFee}, #{payOrder.discountFee}, #{payOrder.freeFee}, " +
            "#{payOrder.couponFee},#{payOrder.recordOrderId}, 1, " +
            "#{payOrder.serviceFee}, #{payOrder.isv}, #{payOrder.aliUserId}, #{payOrder.send}, #{payOrder.deviceId}, #{payOrder.piType}, #{payOrder.actualFee})")
    @SelectKey(statement = "select LAST_INSERT_ID()", keyProperty = "payOrder.id", before = false, resultType = int.class)
    void insertPayOrderMerge(@Param("payOrder") PayOrder payOrder);







    // 获取 已支付成功，未下发到车场的账单
    @Select("select * from payorder where (send is null or send = 0) and payStatus = 1 order by createdTime limit 0, 100")
    List<PayOrder> getUnDownPayOrders();

    // 更新 账单的下发状态
    @Update("update payorder set send = 1 where id = #{id}")
    void updateSendById(@Param("id") int id);

    @Select("select * from payorder where orderNo = #{orderNo}")
    PayOrder getByOrderNo(@Param("orderNo") String orderNo);

    @Select("select id from payorder where orderNo = #{orderNo}")
    Integer getIdByOrderNo(@Param("orderNo") String orderNo);

    @Select("select * from payorder where transactionId = #{transactionId}")
    PayOrder getByTransactionId(@Param("transactionId") String transactionId);

    // 更新账单的支付状态：更新字段：openid ，transactionId，payStatus
    @Update("update payorder set openid = #{payOrder.openid}, transactionId = #{payOrder.transactionId}, " +
            "payStatus = #{payOrder.payStatus}, payTime = #{payOrder.payTime}, " +
            "actualFee = #{payOrder.actualFee}, " +
            "couponFee = #{payOrder.couponFee}, " +
            "cashFee = #{payOrder.cashFee} " +
            "where id = #{payOrder.id}")
    void updatePayOrder(@Param("payOrder") PayOrder payOrder);

    // 查询 已知支付方式，不知支付状态的订单
    @Select("select * from payorder where payStatus = 0 and payWay != 3 order by createdTime limit 0, 100")
    List<PayOrder> getUntreatedPayOrder();

    // 更新支付方式
    @Update("update payorder set payWay = #{payWay} and payStatus = 0 where id = #{id}")
    void updatePayWay(@Param("id") Integer id, @Param("payWay") Integer payWay);

    // 删除5分钟前不知支付方式的订单
    @Delete("delete from payorder where createdTime < #{timeout} and payWay = 3")
    void deleteUnknownPayWayOrder(@Param("timeout") Date timeout);

    @Select("select orderNo from payorder where payStatus = 0 and payWay = 0 and createdTime < #{date} limit 0, 100")
    List<PayOrder> getNeedClosePayOrder(Date date);

    @Select("select * from payorder where orderNo = #{orderNo} limit 1")
    PayOrder selectByOrderNo(@Param("orderNo") String orderNo);

    @Update("update payorder set couponId = #{couponId}, couponFee = #{couponFeeTotal}," +
            "fee = #{fee},send = 0,couponIds = #{ids} "+
            " where orderNo = #{orderNo}")
    void updatePayOrderCouponId(@Param("couponId") Integer couponId,
                                @Param("orderNo") String orderNo,
                                @Param("couponFeeTotal") int couponFeeTotal,
                                @Param("fee") int fee,
                                @Param("ids") String ids);

    @Update("update payorder set payStatus = 1  , couponFee = #{couponFee}," +
            "couponId = #{couponId},actualFee = 0,fee =0,payWay = 7 ,payTime = #{payTime},couponIds = #{ids}  " +
            "where orderNo = #{orderNo}")
    void updatePayOrderPayStatus(@Param("couponId") Integer couponId,
                                 @Param("orderNo") String orderNo,
                                 @Param("couponFee") int couponFee,
                                 @Param("payTime") Date payTime,
                                 @Param("ids") String ids);


    //更新支付失败状态
    @Update("update payorder set payWay = #{payOrder.payWay}, tradeType = #{payOrder.tradeType},payStatus=#{payOrder.payStatus}," +
            "isv = #{payOrder.isv}, apiUserId = #{payOrder.apiUserId} " +
            "where id = #{payOrder.id}")
    void updatePayFailOrderStatus(@Param("payOrder") PayOrder payOrder);


    //更新账单
    @Update("update payorder set payWay = #{payOrder.payWay}, tradeType = #{payOrder.tradeType}, " +
            "isv = #{payOrder.isv}, apiUserId = #{payOrder.apiUserId}, payStatus = #{payOrder.payStatus} " +
            "where id = #{payOrder.id}")
    void updatePayWayInfo(@Param("payOrder") PayOrder payOrder);


    @Select(" SELECT pa.id,pa.orderNo,pa.actualFee,pa.shouldFee,pa.fee,pa.payStatus,pa.couponFee,iop.enter_time AS enterTime,iop.leave_time AS leaveTime,pa.plate,pa.createdTime, " +
            " iop.parking_lot_code AS parkingLotCode,iop.entre_url AS entreUrl,iop.leave_url AS leaveUrl,pa.parkingId " +
            " FROM payorder AS pa LEFT JOIN road_in_out_parking AS iop ON pa.roadRecordId = iop.id " +
            " WHERE plate = #{plate} AND payStatus = #{type} AND isAbnormal IS NOT NULL  AND isAbnormal != 1 ORDER BY pa.createdTime DESC ")
    List<RoadPayOrderVo> selectRoadPayOrderList(@Param("plate") String plate,@Param("type") Integer type);

    @Select("<script>" +
            "select * from payorder where  payStatus = 1 and fee != 0 and recordOrderId   in  " +
            " <foreach item=\"item\" index=\"index\" collection=\"recordOrderIds\" open=\"(\" separator=\",\" close=\")\">  \n" +
            "  #{item}  " +
            " </foreach>  " +
            "</script>")
    List<PayOrder> getRecordOrderId(@Param("recordOrderIds") List<String> recordOrderIds);

    @Select("select distinct payWay from payorder where parkingId = #{parkingId} and infoType = 3 and infoId = #{infoId}")
    List<Integer> listDistinctPayWayByFeeDetailId(@Param("infoId") int localFeedetailId, @Param("parkingId") Integer parkingId);

}
